<?php


namespace app\shop\controller\admin;


use app\shop\common\consts\Consts;
use app\shop\controller\BaseAdminController;
use app\shop\model\user\AdminRoleModel;
use app\shop\model\user\DepartmentModel;
use app\shop\model\user\FriendModel;
use app\shop\model\user\GroupUserModel;
use app\shop\model\user\UserModel;
use app\shop\service\user\AdminRoleService;
use app\shop\service\user\UserService;

class ServiceController extends BaseAdminController
{

    public function setModel()
    {
        return new UserModel();
    }

    function setSearch()
    {
        $where = [];
        $where[] = ['role_id', 'in', [Consts::SERVICE_LEADER_ID]];
        return [
            'like' => ['realname', 'mobile'],
            'custom' => $where
        ];
    }

    function setSort()
    {
        return [
            'role_name' => 'role_id'
        ];
    }


    /**
     * 获取用户信息
     * @return \think\response\Json
     */
    public function info()
    {
        try {
            $userInfo = $this->request->middleware('userInfo');
            unset($userInfo['password']);
            return $this->success($userInfo);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 设置登录用户信息
     * @return \think\response\Json
     */
    public function setUserInfo()
    {
        try {
            $nick = $this->request->param('nick', '', 'trim');
            $avatar = $this->request->param('avatar', '', 'trim');
            $pwd = $this->request->param('password', '', 'trim');

            $userInfo = $this->request->middleware('userInfo');
            $userInfo->avatar = $avatar;
            $userInfo->nick = $nick;
            if ($pwd) {
                $userInfo->password = password_hash($pwd . $userInfo->salt, PASSWORD_DEFAULT);
            }
            $userInfo->save();
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 会员列表
     * @return \think\response\Json
     */
    public function getUserList()
    {
        try {
            $where = $this->searchWhere;
            $list = UserModel::where($where)
                ->order($this->orderBy, $this->sort)
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as &$v) {
                $v['create_time'] = date('Y-m-d H:i', strtotime($v['create_time']));
                $v['last_login_time'] = date('Y-m-d H:i', $v['last_login_time']);
                $v['sex_name'] = UserService::getGender($v['sex']);
//                $v['role_name'] = AdminRoleModel::where('id',$v['role_id'])->value('role_name') ?? '普通用户';
                $v['department_name'] = DepartmentModel::where('id', $v['department_id'])->value('name', '');
                $v['location'] = $v['last_login_ip'] ? implode(" ", \Ip::find($v['last_login_ip'])) : '--';
                $v['reg_location'] = $v['register_ip'] ? implode(" ", \Ip::find($v['register_ip'])) : '--';
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 获取用户好友列表
     * @return \think\response\Json
     */
    public function getUserFriendList()
    {
        try {
            $id = $this->request->param('user_id', 0, 'int');
            $list = FriendModel::where('create_user', $id)
                ->where('status', 1)
                ->order('create_time', 'desc')
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as &$v) {
                $v['create_time'] = date('Y-m-d H:i', strtotime($v['create_time']));
                $v['friend_user_name'] = UserModel::where('user_id', $v['friend_user_id'])->value('realname', '');
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 修改用户状态
     * @return \think\response\Json
     */
    public function setUserStatus()
    {
        try {
            $id = $this->request->param('id', 0, 'int');
            $status = $this->request->param('status', 0, 'int');
            if ($id <= 0) {
                throw new \Exception(lang(1001));
            }
            $userInfo = UserModel::find($id);
            if (!$userInfo) {
                throw new \Exception(lang(1002));
            }
            $userInfo->status = $status ? 1 : 0;
            $userInfo->save();
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 添加修改用户
     * @return \think\response\Json
     */
    public function add()
    {
        try {
            $data = $this->request->param('', '', 'trim');
            $data['user_sn'] = $data['account'];
            if (!$data['user_id']) {
                $data['salt'] = random(4);
                $data['password'] = password_hash($data['password'] . $data['salt'], PASSWORD_DEFAULT);
            } else {
                $model = $this->setModel()->find($data['user_id']);
                if ($data['password']) {
                    $data['password'] = password_hash($data['password'] . $model->salt, PASSWORD_DEFAULT);
                }
            }
            parent::save($data);
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 设计人员列表
     * @return \think\response\Json
     */
    public function getDesignUserArr()
    {
        try {
            $list = UserModel::where('role_id', Consts::DESIGN_ID)
                ->where('status', 1)
                ->field('user_id,realname')
                ->select()
                ->toArray();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 销售人员列表
     * @return \think\response\Json
     */
    public function getSaleUserArr()
    {
        try {
            $list = UserModel::where('role_id', Consts::SALE_ID)
                ->where('status', 1)
                ->field('user_id,realname')
                ->select()
                ->toArray();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function setNamespace()
    {
        // TODO: Implement setNamespace() method.
    }

    public function setWithModel()
    {
        // TODO: Implement setWithModel() method.
    }
}